package cron

import (
	fileModel "blog/app/model/file"
	pixiv2 "blog/app/model/pixiv"
	fileServices "blog/app/services/system/file"
	"blog/core"
	"blog/core/cache"
	"blog/utils/pixiv"
	"blog/utils/upload"
	"fmt"
	"net/http"
)

func PhotoOfTheDay() {
	if !core.Cache.Exists("photo_of_the_day") {
		data := pixiv.New().Send().Get()
		v := data[0]
		var info fileModel.Config
		core.DbConnect.Model(fileModel.Config{}).Where("name = ?", "qiniu").Find(&info)
		client := &http.Client{}
		url := v.Url
		req, _ := http.NewRequest("GET", url, nil)
		resp, err := client.Do(req)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		defer resp.Body.Close()
		tf := upload.NewFile(url, resp.ContentLength, resp.Body, upload.Set.SetMineType(resp.Header.Get("Content-Type")), upload.Set.SetTitle(v.Title))
		create, _ := fileServices.File{}.Upload(tf, false)
		core.DbConnect.Create(&pixiv2.Pixiv{
			Pid:   v.Pid,
			Uid:   v.Uid,
			Title: v.Title,
			Url:   create.Hash,
			R18:   0,
		})

		core.Cache.Set("photo_of_the_day", v.Url, cache.Set.SetExpire(3600))
	}
}
